package com.zzc.action;

import com.alibaba.fastjson.JSONObject;
import com.zzc.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.zzc.bean.Employee;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import com.zzc.service.EmployeeService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
public class EmployeeAction extends BaseAction{

	public EmployeeAction() {
		super();
	}
	private EmployeeService employeeService;
	public EmployeeService getEmployeeService() {
		return employeeService;
	}
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	public void initData() throws ServletException, IOException {
		employeeService.init();
		getAll();
	}
	public void add() throws IOException {
		employeeService.addEmployee();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.sendRedirect(getPath()+"/Action/Action_EmployeeAction_getAll");
//		return "getStudents";
	}
	public void delete() throws IOException {
		employeeService.delete();
	}
	public void alter() throws IOException {
		employeeService.alter();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.sendRedirect(getPath()+"/Action/Action_EmployeeAction_getAll");
	}
	public void get_id() throws ServletException, IOException {
		employeeService.get_id();
	}
	public void alter_get_account() throws ServletException, IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		Employee employee_alter=employeeService.alter_get_account();
		request.setAttribute("employee_alter",employee_alter);
		request.getRequestDispatcher("/view/employee/employee_alter.jsp").forward(request, getResponse());
	}
	public void getAll() throws ServletException, IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Employee> list_employees = employeeService.getAll();
		request.setAttribute("list_employees", list_employees);
		request.getRequestDispatcher("/view/admin/admin_employee.jsp").forward(request, getResponse());
	}
	public void requestLogin() throws IOException, ServletException {
        HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.sendRedirect(request.getContextPath()+"/view/index.jsp");
	}
	public void checkUser() {
		employeeService.checkUser();
	}
	public void login() throws IOException, ServletException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String result = employeeService.login();
//		if (result.equals("admin")){
//			request.getRequestDispatcher("/view/admin/admin_main.jsp").forward(request, getResponse());
//		}
//		else {
//            request.getRequestDispatcher("/view/admin/admin_main.jsp").forward(request, getResponse());
//		}
		response.sendRedirect(request.getContextPath()+"/view/admin/index.jsp");
	}
	public void login_json() throws IOException, ServletException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String result = employeeService.login();
//		JSONObject jsonObject =JSONObject.parseObject(JSONObject.toJSON(employee).toString());
		BaseService.printAll_string(result);
	}
	public void login_out() throws IOException, ServletException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		request.getSession().removeAttribute("employee");
		response.sendRedirect(request.getContextPath()+"/view/index.jsp");
	}
}
